

((_global_)=>{
	if(!Reflect.has(_global_, 'mtec')){
		Reflect.set(_global_, 'mtec', {});
		Reflect.set(mtec, '__mtec__', 'Many technologies');
	}

	if(!mtec.local){
		Reflect.set(mtec, 'local', {});
	}
	Reflect.set(mtec.local, '__space_description__', 'This is a mtec subnamespace for local');
})(window ?? globalThis);

mtec.local.read = function(key, out){
	let data_json = localStorage.getItem(key.encodeBtoA);

	if(data_json){
		let data: any;
		try{
			data = JSON.parse(data_json.decodeAtoB);
		}catch(e){
			data = undefined;
			console.warn('[mtec.local.read] Error parsing JSON data from localStorage:', e);
		}

		if(data){
			if(out){
				mtec.fusionData(out, data);
			}else{
				out = data;
			}
			data = null;
		}
	}

	return out as any;
}

mtec.local.save = function(key, data){
	let json_str: string;
	try{
		json_str = JSON.stringify(data);
		localStorage.setItem(key.encodeBtoA, json_str.encodeBtoA);
	}catch(e){
		console.warn('[mtec.local.save] Error saving data to localStorage:', e);
	}

	json_str = undefined;
}